package Thread;

import java.io.*;

class myWriter extends Thread{
    private PipedOutputStream outStream;    //将数据输出
    private String messages[] = {"Monday", "Tuesday","Wednsday", "Thursday","Friday","Saturday","Sunday"};
    public myWriter(PipedOutputStream o){
        outStream = o;
    }

    public void run(){                   //myWriter这个线程往管道流里输出信息
        PrintStream p = new PrintStream(outStream);
        for(int i=0;i<messages.length;i++){
            p.println(messages[i]);
            p.flush();
            System.out.println("Write: " + messages[i]);
        }
        p.close();
        p=null;
    }
}
class myReader extends Thread{          //myReader辅类  线程
    private PipedInputStream inStream;
    public myReader(PipedInputStream i){        //
        inStream = i;
    }
    public void run(){          //从管道流中读入信息
        String line;
        boolean reading = true;
        BufferedReader d = new BufferedReader(new InputStreamReader(inStream));
        while(reading && d!=null){
            try{
                line =d.readLine();
                if(line != null) System.out.println("Read: " +line);
            }catch (IOException e){}
        }try{
            Thread.currentThread().sleep(4000);
        }catch(InterruptedException e){}
    }
}

public class Pipethread {
    public static void main(String[] args) {
        Pipethread thisPipe = new Pipethread();
        thisPipe.process();
    }
    public void process(){
        PipedOutputStream outStream;
        PipedInputStream inStream;
        try{
            outStream = new PipedOutputStream();
            inStream = new PipedInputStream(outStream);
            new myWriter(outStream).start();        //输出流作为参数传给myWriter
            new myReader(inStream).start();         //输入流作为参数传给myReader
        }catch(IOException e ){}
    }
}
